package com.opener.common.utils;

/**
 * @author baboy
 */
public class Radix64 {
    final static char[] digits = "qHCMOBJ1TVQj2fy6YPclnIxwZd0sk8pGrAN3hug7WFKEmoXD54U_L9RvtzSb-iea".toCharArray();

    /**
     * 把10进制的数字转换成64进制
     *
     * @param number
     * @return
     */
    public static String toString(long number) {
        char[] buf = new char[64];
        int charPos = 64;
        int radix = 1 << 6;
        long mask = radix - 1;
        do {
            buf[--charPos] = digits[(int) (number & mask)];
            number >>>= 6;
        } while (number != 0);
        return new String(buf, charPos, (64 - charPos));
    }

    /**
     * 把64进制的字符串转换成10进制
     *
     * @param s
     * @return
     */
    public static long parseLong(String s) {
        long result = 0;
        for (int i = s.length() - 1; i >= 0; i--) {
            for (int j = 0; j < digits.length; j++) {
                if (s.charAt(i) == digits[j]) {
                    result += ((long) j) << 6 * (s.length() - 1 - i);
                }
            }
        }
        return result;
    }

    /**
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i< 100; i++){
            long timeMillins = System.currentTimeMillis();
            System.out.println("初始值:" + timeMillins);
            String tmp = toString(timeMillins);
            System.out.println("加密后:" + tmp);
            System.out.println("解密后:" + parseLong(tmp));

        }
    }

}
